package sort.test3_30;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class BucketSort1 {

    public  void sort(int[] nums){
        if (nums.length==0){
            return;
        }
        int min=Integer.MAX_VALUE;
        int max=Integer.MIN_VALUE;
        int numsLength = nums.length;

        for (int i = 0; i < numsLength ; i++) {
            min=Math.min(min,nums[i]);
            max=Math.max(max,nums[i]);
        }

        int bucketCount=(max-min)/numsLength+1;

        ArrayList<ArrayList<Integer>> lists = new ArrayList<>();

        for (int i = 0; i < bucketCount; i++) {
            lists.add(new ArrayList<>());
        }


        for (int i = 0; i < nums.length; i++) {
            lists.get((nums[i]-min)/numsLength).add(nums[i]);
        }

        for (int i = 0; i < bucketCount; i++) {
            Collections.sort(lists.get(i));
        }
        int temp=0;
        for (int i = 0; i < bucketCount; i++) {
            for (int j = 0; j < lists.get(i).size(); j++) {
                nums[temp++]=lists.get(i).get(j);

            }
        }
    }

    public static void main(String[] args) {
        BucketSort1 bucketSort1 = new BucketSort1();
        int[] arr = {3,44,38,5,47,15,36,26,27,2,46,4,19,50,48};
        bucketSort1.sort(arr);
        System.out.println(Arrays.toString(arr));
    }


}
